@echo off
echo.
echo 🚀 Poppler 源码编译脚本
echo =====================================

:: Set colors
set "GREEN=[92m"
set "RED=[91m"
set "YELLOW=[93m"
set "BLUE=[94m"
set "NC=[0m"

:: Check for Qt and MinGW
set "QT_ROOT=C:\Qt\6.9.2\mingw_64"
if not exist "%QT_ROOT%" (
    echo %RED%❌ Qt MinGW not found at %QT_ROOT%%NC%
    pause
    exit /b 1
)

echo %GREEN%✅ Using Qt at: %QT_ROOT%%NC%
set "PATH=%QT_ROOT%\bin;C:\Qt\Tools\mingw1310_64\bin;C:\Qt\Tools\CMake_64\bin;%PATH%"

:: Clean and create build directory
echo.
echo %BLUE%🧹 清理Poppler构建目录...%NC%
if exist "poppler_build" rmdir /s /q "poppler_build"

echo %BLUE%📁 创建Poppler构建目录...%NC%
mkdir "poppler_build"
cd "poppler_build"

:: Check for MSYS2 and install dependencies if available
echo %BLUE%🔍 检查依赖项...%NC%
if exist "C:\msys64\mingw64\bin\pkg-config.exe" (
    echo %GREEN%✅ 发现MSYS2，尝试安装Freetype%NC%
    C:\msys64\usr\bin\pacman.exe -S --noconfirm mingw-w64-x86_64-freetype >nul 2>&1
    C:\msys64\usr\bin\pacman.exe -S --noconfirm mingw-w64-x86_64-libjpeg-turbo >nul 2>&1
    C:\msys64\usr\bin\pacman.exe -S --noconfirm mingw-w64-x86_64-libpng >nul 2>&1
    set "PKG_CONFIG_PATH=C:\msys64\mingw64\lib\pkgconfig"
    set "PATH=C:\msys64\mingw64\bin;%PATH%"
)

:: Configure Poppler with minimal dependencies for Windows/MinGW
echo.
echo %BLUE%⚙️ 配置 Poppler CMake 项目 (MinGW)...%NC%
cmake -G "MinGW Makefiles" ^
    -DCMAKE_BUILD_TYPE=Release ^
    -DCMAKE_INSTALL_PREFIX="%CD%\..\poppler_install" ^
    -DBUILD_SHARED_LIBS=ON ^
    -DENABLE_CPP=ON ^
    -DENABLE_GLIB=OFF ^
    -DENABLE_QT5=OFF ^
    -DENABLE_QT6=OFF ^
    -DENABLE_BOOST=OFF ^
    -DBUILD_GTK_TESTS=OFF ^
    -DBUILD_QT5_TESTS=OFF ^
    -DBUILD_QT6_TESTS=OFF ^
    -DBUILD_CPP_TESTS=OFF ^
    -DENABLE_UTILS=OFF ^
    -DENABLE_SPLASH=ON ^
    -DENABLE_LIBOPENJPEG=none ^
    -DENABLE_CMS=none ^
    -DENABLE_DCTDECODER=libjpeg ^
    -DWITH_JPEG=ON ^
    -DWITH_PNG=ON ^
    -DWITH_TIFF=OFF ^
    -DWITH_NSS3=OFF ^
    -DWITH_Cairo=OFF ^
    -DRUN_GPERF_IF_PRESENT=OFF ^
    -DTESTDATADIR=IGNORE ^
    ..\poppler

if errorlevel 1 (
    echo %RED%❌ Poppler CMake 配置失败%NC%
    pause
    exit /b 1
)

echo %GREEN%✅ Poppler CMake 配置成功%NC%

:: Build Poppler
echo.
echo %BLUE%🔨 开始编译Poppler (Release)...%NC%
mingw32-make -j4

if errorlevel 1 (
    echo %RED%❌ Poppler 编译失败%NC%
    pause
    exit /b 1
)

echo %GREEN%✅ Poppler 编译成功%NC%

:: Install Poppler
echo.
echo %BLUE%📦 安装Poppler...%NC%
mingw32-make install

if errorlevel 1 (
    echo %RED%❌ Poppler 安装失败%NC%
    pause
    exit /b 1
)

cd ..
echo.
echo %GREEN%🎉 Poppler 编译和安装完成！%NC%
echo %GREEN%📍 安装位置: poppler_install%NC%
echo.

:: Copy essential DLLs to a lib directory for easier linking
echo %BLUE%🔧 整理库文件...%NC%
if not exist "poppler_install\lib" mkdir "poppler_install\lib"
copy "poppler_build\*.dll" "poppler_install\lib\" >nul 2>&1
copy "poppler_build\*.a" "poppler_install\lib\" >nul 2>&1
copy "poppler_build\cpp\*.dll" "poppler_install\lib\" >nul 2>&1
copy "poppler_build\cpp\*.a" "poppler_install\lib\" >nul 2>&1

echo %GREEN%✅ 库文件整理完成%NC%
echo.
pause